<?php
/**
* 2017年3月17日
* 管理员类
*/
class Admin_model extends MY_Model
{
	private $table = 'admin';

	public $_config_ = array(
			'status' => array(
					'1'  => '正常',
					'0'  => '冻结',
					'-1' => '删除',
				),
			'sex' => array(
					'1' => '男',
					'2' => '女',
				)
		);
	function __construct()
	{
		parent::__construct();
	}

	/**
	 * 获取管理员信息 用户登入
	 */
	function getAdmin_login($post){
		$where = array(
			'account'=>$post['account'],
			'password'=>md5($post['password']),
			);	
		$admininfo = $this->hw_getRecord($this->table,$where,'*');
		return $admininfo;
	}

	/**
	 * 获取管理员信息
	 */
	function getAdminById($id){
		$where = array(
				'id'  =>  $id,
				'status' => 1,
			);	
		$admininfo = $this->hw_getRecord($this->table,$where);
		return $admininfo;
	}

	/**
	 * 获取管理员列表
	 */
	function getAdminList($where=array('status'=>1)){
		$adminlist = $this->hw_getList($this->table,$where);
		$grouplist = $this->hw_getList('authgroup',array('status'=>1));
		foreach($adminlist as $k=>$admin){
			foreach($grouplist as $group){
				if($admin['role'] == $group['id']){
					$adminlist[$k]['role_name'] = $group['name'];
				}
			}
		}
		return $adminlist;
	}
	/**
  	 * 获取顾问列表
  	 * @DateTime 2017-09-01
  	 * @author xyl
  	 * @param  [type] $where  [description]
  	 * @param  [type] $search [description]
 	  * @return [type]         [description]
  	 */
  	function getAddressList($where,$search)
  	{
  		$like = array('username'=>$search);
   		$list = $this->hw_getList_like($this->table,$where,'*',$like);
   		return $list;
  	}

	/**
	 * 处理添加或编辑管理员操作
	 * @return [type] [description]
	 */
	function handleAddAdmin($post){

		if($post['id']==0){
			//新增管理员
			//检查帐号是否已经存在
			$where = array('account'=>$post['account']);
			$admin = $this->hw_getRecord($this->table,$where,'id');
			if($admin){
				//帐号已经存在 跳转回去并提示帐号存在
				$return=array('code'=>0,'msg'=>'账号已经存在');
			}else{
				//处理添加管理员流程
				$return = $this->addAdmin($post);
			}
		}else{
			//编辑管理员
			$return = $this->editAdmin($post);
		}
		return $return;
	}


	/**
	 * 处理新增管理员
	 * @param  [type] $post       [description]
	 * @return [type]             [description]
	 */
	function addAdmin($post){
		//管理员信息
		$data = array(
			'username'  =>  $post['username'],
			'account'   =>  $post['account'],
			'password'  =>  md5($post['password']),
			'role'      =>  $post['role'], 
			// 'authority' =>  implode(',',$post['authority']),
			'sex'       =>  $post['sex'],
			'phone'    =>  $post['phone'],   				
			);
		$admin_add = $this->hw_Insert($this->table,$data);
		if($admin_add){
			$return=array('code'=>1,'msg'=>'添加成功');
		}else{
			$return=array('code'=>0,'msg'=>'添加失败');
		}
		//添加操作日志
		$this->logMdl->addOperationLog($return['code'],1,$admin_add,$this->table,'管理员',json_encode($post));
		return $return;
	}

	/**
	 * 处理编辑管理员
	 * @param  [type] $post       [description]
	 * @return [type]             [description]
	 */
	function editAdmin($post){
		//编辑管理员信息
		$data = array('update_time'=>date('Y-m-d H:i:s'));
		if($post['password']!=''){
			$data['password']=md5($post['password']);
		}
		// $data['authority'] = implode(',',$post['authority']);
		$data['username']  = $post['username'];
		$data['role']  = $post['role'];
		$data['sex']  = $post['sex'];
		$data['phone']  = $post['phone'];
		$where['id'] = $post['id'];
		$before_data = $this->getAdminById($post['id']);
		$admin_edit = $this->hw_Update($this->table,$data,$where);
		if($admin_edit>0){
			$return=array('code'=>1,'msg'=>'编辑成功');
		}else{
			$return=array('code'=>0,'msg'=>'编辑失败');
		}
		$log_details = array('before'=>$before_data,'after'=>$post);
		//添加操作日志
		$this->logMdl->addOperationLog($return['code'],2,$post['id'],$this->table,'管理员',json_encode($log_details));
		return $return;
	}
	
	/**
	 * 状态设置
	 * @DateTime 2017-07-03
	 * @author zyl
	 * @param    [type]     $post [description]
	 */
	function setStatus($post,$set){

		$ids = $post['ids'];
		$data = array(
				'status' => $value
			);
		$row = $this->hw_Update_setStatus($this->table,$ids,$set);

		return $row;

	}

	/**
	 * 更新管理员信息(多条)
	 * @DateTime 2017-07-19
	 * @author zyl
	 * @param    [type]     $data  [更新数据]
	 * @param    [type]     $where [条件]
	 * @param    integer    $type  [更新类别(0:删除,2:编辑,3:审核)]
	 * @return   [type]            [description]
	 */
	function updateAdmin_in($data,$ids,$type=2){
		$i=0;
		foreach($ids as $id){
			$where['id'] = $id;
			$info = $this->hw_getRecord($this->table,$where);
			$res = $this->hw_Update($this->table,$data,$where);
			if($res){
				$i++;
				//添加操作日志
				$this->logMdl->addOperationLog(1,$type,$id,$this->table,'管理员',json_encode($info));
			}else{
				//添加操作日志
				$this->logMdl->addOperationLog(0,$type,$id,$this->table,'管理员',json_encode($info));
			}
		}
		if($i>0){
			
			$return = array('code'=>1,'msg'=>'操作成功');
		}else{
			
			$return = array('code'=>0,'msg'=>'操作失败');
		}
		return $return;
		// return $this->hw_Update_in($this->table,$data,'id',$ids);
	}
}